{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f04dd603-a2d1-48ce-8c17-9f1dba8de1ee",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# 极坐标、平面直角坐标\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccafb456-2453-4c82-8a65-b1963a370cb2",
   "metadata": {},
   "source": [
    "这段代码的核心目标是 **在二维平面中实现直角坐标系与极坐标系的相互转换**。数学上，直角坐标 $(x_1, x_2)$ 可以转换为极坐标 $(r, \\theta)$，反之亦然。具体分析如下：\n",
    "\n",
    "---\n",
    "\n",
    "### 1. 直角坐标转极坐标\n",
    "\n",
    "#### (a) 计算极径 $r$\n",
    "\n",
    "在极坐标系中，极径（radius）$r$ 表示点 $(x_1, x_2)$ 到原点的欧几里得距离，计算公式为：\n",
    "\n",
    "$$\n",
    "r = \\sqrt{x_1^2 + x_2^2}\n",
    "$$\n",
    "\n",
    "对于代码中的点 $(3,4)$，计算如下：\n",
    "\n",
    "$$\n",
    "r = \\sqrt{3^2 + 4^2} = \\sqrt{9 + 16} = \\sqrt{25} = 5\n",
    "$$\n",
    "\n",
    "Python 代码 `np.sqrt(x1**2 + x2**2)` 计算了这个结果并存储在变量 `r` 中。\n",
    "\n",
    "#### (b) 计算极角 $\\theta$\n",
    "\n",
    "极角 $\\theta$ 是从 $x$ 轴正方向逆时针旋转到向量 $(x_1, x_2)$ 所形成的角度，其计算公式为：\n",
    "\n",
    "$$\n",
    "\\theta = \\arctan \\left(\\frac{x_2}{x_1} \\right)\n",
    "$$\n",
    "\n",
    "为了正确处理四象限情况，代码使用 `np.arctan2(x2, x1)`，其计算方式等价于：\n",
    "\n",
    "$$\n",
    "\\theta = \\arctan2(4, 3)\n",
    "$$\n",
    "\n",
    "由此计算得到的 $\\theta$ 角度为（以弧度表示）：\n",
    "\n",
    "$$\n",
    "\\theta \\approx 0.93 \\text{ radians}\n",
    "$$\n",
    "\n",
    "将其转换为角度（度数）时，使用 `np.rad2deg(theta)` 进行转换：\n",
    "\n",
    "$$\n",
    "\\theta_{\\text{deg}} = \\frac{180}{\\pi} \\cdot \\theta \\approx 53.13^\\circ\n",
    "$$\n",
    "\n",
    "因此，点 $(3,4)$ 在极坐标系中的表示为 $(r, \\theta) = (5, 53.13^\\circ)$。\n",
    "\n",
    "---\n",
    "\n",
    "### 2. 极坐标转直角坐标\n",
    "\n",
    "接下来，我们使用极坐标 $(r, \\theta)$ 重新计算直角坐标 $(x_1, x_2)$，验证转换的正确性。\n",
    "\n",
    "#### (a) 计算 $x_1$\n",
    "\n",
    "根据极坐标到直角坐标的转换公式：\n",
    "\n",
    "$$\n",
    "x_1 = r \\cos \\theta\n",
    "$$\n",
    "\n",
    "代码计算了 `x1_ = r * np.cos(theta)`，代入计算：\n",
    "\n",
    "$$\n",
    "x_1 = 5 \\cos(0.93) \\approx 3.0\n",
    "$$\n",
    "\n",
    "#### (b) 计算 $x_2$\n",
    "\n",
    "同样，根据公式：\n",
    "\n",
    "$$\n",
    "x_2 = r \\sin \\theta\n",
    "$$\n",
    "\n",
    "代码计算 `x2_ = r * np.sin(theta)`，代入计算：\n",
    "\n",
    "$$\n",
    "x_2 = 5 \\sin(0.93) \\approx 4.0\n",
    "$$\n",
    "\n",
    "这样，我们成功地将极坐标 $(5, 53.13^\\circ)$ 转换回了直角坐标 $(3,4)$，验证了坐标转换的正确性。\n",
    "\n",
    "---\n",
    "\n",
    "### 结论\n",
    "\n",
    "1. **直角坐标 $(x_1, x_2)$ 转换为极坐标 $(r, \\theta)$**：\n",
    "   - 计算极径 $r = \\sqrt{x_1^2 + x_2^2}$。\n",
    "   - 计算极角 $\\theta = \\arctan2(x_2, x_1)$。\n",
    "\n",
    "2. **极坐标 $(r, \\theta)$ 转换回直角坐标 $(x_1, x_2)$**：\n",
    "   - 计算 $x_1 = r \\cos \\theta$。\n",
    "   - 计算 $x_2 = r \\sin \\theta$。\n",
    "\n",
    "3. **转换验证**：\n",
    "   - 计算的直角坐标 $(x_1, x_2) = (3,4)$ 与原始输入一致，说明计算正确。\n",
    "   - 该转换在计算机图形学、机器人学、物理建模等领域有重要应用。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47a92590-124c-41da-be31-a89d7710c060",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bd85e011-fe43-4b78-942b-1a78a44561cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e34563c8-6aa5-4001-8683-8c5d654eb30d",
   "metadata": {},
   "source": [
    "## 平面直角坐标转化为极坐标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d30e4699-aca4-411b-81f1-c1e2c869fa11",
   "metadata": {},
   "outputs": [],
   "source": [
    "x1, x2 = 3, 4  # 直角坐标 (3, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "27c631a2-39cb-4b7e-bba1-d0b10f1d744a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.0"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r = np.sqrt(x1**2 + x2**2)  # 计算极径 r\n",
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "ed5bf69d-d5d1-4c71-aba8-d32ee05d9a6a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9272952180016122"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "theta = np.arctan2(x2, x1)  # 计算极角 theta\n",
    "theta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e08dc6e3-aa10-4ac3-93dc-92b2d8184ed5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "53.13010235415598"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.rad2deg(theta)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d30b11eb-b6d4-40bb-ab67-ec9b5cbef25c",
   "metadata": {},
   "source": [
    "## 极坐标转化为平面直角坐标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "986911fd-7100-44d1-bbf8-71a048891ad6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0000000000000004"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x1_ = r * np.cos(theta)\n",
    "x1_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "15f37472-dad1-4dba-92f1-f51a203021eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.9999999999999996"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x2_ = r * np.sin(theta)\n",
    "x2_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d530bc5e-a5b5-41cf-92e4-461541be8fc2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "31d089c2-47a9-410c-a633-955540451cda",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
